package com.xingshang.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xingshang.mapper.CartMapper;
import com.xingshang.mapper.GoodsMapper;
import com.xingshang.model.dto.CartItem;
import com.xingshang.model.po.Cart;
import com.xingshang.model.po.Goods;
import com.xingshang.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CartServiceImpl  extends ServiceImpl<CartMapper, Cart> implements CartService  {
    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public int addCart(Cart cart) {
//        System.out.println("userId: " + userId + "  goodsId: " + goodsId + "  quantity: " + quantity);
        return cartMapper.addCart(cart);
    }

    @Override
    public List<Cart> getCartByUserId(int id) {
        return cartMapper.getCartByUserId(id);
    }

    @Override
    public Cart getCartItemByUserIdAndGoodsId(Integer userId, Integer goodsId) {
        return cartMapper.getCartItemByUserIdAndGoodsId(userId, goodsId);
    }

    @Override
    public List<Cart> getCartByUserIdAndOrderId(int id,int order_id) {
        return cartMapper.getCartByUserIdAndOrderId(id,order_id);
    }
    @Override
    public void updateCartItem(Cart cartItem) {
        this.updateById(cartItem);
    }

    @Override
    public void addCartItem(Cart cartItem) {
        this.save(cartItem);
    }

    @Override
    public List<CartItem> selectByUserId(Integer userId) {
        return cartMapper.findCartByUserId(userId);
    }

    @Override
    public void clearCart(Integer userId,Integer orderId) {
        cartMapper.clearCart(userId);
//        cartMapper.clearCarItem(orderId);
    }

    @Override
    public List<Cart> findGoodsByCart(List<Cart> cartList) {
        for (Cart cart : cartList) {
            Goods goods = goodsMapper.selectById(cart.getGoodsId());
            if(goods != null) {
                cart.setName(goods.getName());
                cart.setSalePrice(goods.getSalePrice());
                cart.setImage(goods.getImage());
            }
        }
        return cartList;
    }

    @Override
    public int deleteCartById(Integer id) {
        return cartMapper.deleteCartById(id);
    }

    @Override
    public int updateOrderId(Integer cartId, Integer orderId) {
        return cartMapper.updateOrderId(cartId, orderId);
    }

    @Override
    public Cart getCartByUserIdAndGoodsId(Integer userId, Integer goodsId) {
        return cartMapper.getCartByUserIdAndGoodsId(userId, goodsId);
    }


    @Override
    public int updateCartQuantity(Integer cartId, Integer quantity) {
        return cartMapper.updateCartQuantity(cartId, quantity);
    }


}
